package com.qd.upload.service;

import com.cdqidi.util.DateUtil;
import com.cdqidi.util.FileUtil;
import com.qd.upload.config.UploadDTO;
import com.qd.upload.constant.FileConfirmDTO;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;
import java.nio.file.Files;
import java.time.LocalDate;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class FileAsyncService {
    private final StringRedisTemplate stringRedisTemplate;
    private final UploadDTO uploadDTO;

    @Async
    @SneakyThrows
    public void deleteFiles(List<FileConfirmDTO> list) {
        File file;
        for (FileConfirmDTO fileConfirmDto : list) {
            file = new File(fileConfirmDto.getTempFile());
            log.info("删除文件： {},状态: {}", file.getPath(), Files.deleteIfExists(file.toPath()));
            if (StringUtils.hasLength(fileConfirmDto.getFileId())) {
                Boolean b = stringRedisTemplate.delete(fileConfirmDto.getFileId());
                log.info("删除redis,key： {},状态: {}", fileConfirmDto.getFileId(), b);
            }
            fileConfirmDto.freeData();
        }
    }

    @Async
    @SneakyThrows
    public void deleteFile(FileConfirmDTO fileConfirmDto) {
        final File file = new File(fileConfirmDto.getTempFile());
        log.info("删除文件： {},状态: {}", file.getPath(), Files.deleteIfExists(file.toPath()));
        if (StringUtils.hasLength(fileConfirmDto.getFileId())) {
            Boolean b = stringRedisTemplate.delete(fileConfirmDto.getFileId());
            log.info("删除redis,key： {},状态: {}", fileConfirmDto.getFileId(), b);
        }
        fileConfirmDto.freeData();
    }

    @Async
    public void deleteDirectory() {
        final LocalDate localDate = LocalDate.now();
        final int year = localDate.getYear();
        final int month = localDate.getMonth().getValue();
        log.info("year: {},month:{},now: {}", year, month, localDate);
        final String tempPath = uploadDTO.getTempPath();
        File file = new File(tempPath + File.separator + year);
        final File[] files = file.listFiles();
        if (null != files) {
            for (File m : files) {
                if (Integer.parseInt(m.getName()) < month) {
                    FileUtil.deleteNullChildDirectory(m);
                }
                File[] listFiles = m.listFiles();
                if (null != listFiles) {
                    for (File listFile : listFiles) {
                        try {
                            if (DateUtil.strToLocalDate(listFile.getName()).isBefore(localDate)) {
                                FileUtil.deleteDirectory(listFile);
                            }
                        } catch (Exception ignored) {
                        }
                        FileUtil.deleteNullChildDirectory(listFile);
                    }
                }
                FileUtil.deleteNullChildDirectory(m);
            }
        }
        file = new File(tempPath);
        final File[] listFiles = file.listFiles();
        if (null != listFiles) {
            for (File listFile : listFiles) {
                try {
                    if (Integer.parseInt(listFile.getName()) < year) {
                        FileUtil.deleteDirectory(listFile);
                    }
                } catch (Exception ignored) {
                }
                FileUtil.deleteNullChildDirectory(listFile);
            }
        }
    }
}
